<cfset request.starttick = GetTickCount()>
<cfset var.start = CreateODBCdateTime(now())>

<cfsetting enablecfoutputonly="yes">
<!--- Default the standard variables --->
<cfparam name="attributes.url" default="#application.xtiurl#">
<cfparam name="attributes.dtd" default="#application.xtidtd#">
<cfparam name="attributes.debug" default="false">
<cfparam name="attributes.debuglevel" default="1,2,3">
<cfparam name="attributes.xti_timer" default="1">
<cfparam name="attributes.post" default="">
<cfparam name="attributes.returnvar" default="#attributes.post#">
<cfparam name="attributes.version" default="1">
<cfparam name="attributes.user_Id" default="">
<cfparam name="attributes.arraylist" default="">

<!--- include the xml input code --->
<cfinclude template="input_#trim(attributes.post)#.cfm">

<!--- Make the http call to max --->
<cfhttp url="#attributes.url#" method="POST" timeout="60" throwonerror="yes" redirect="no">
	<cfhttpparam name="xmldoc" value="#trim(xml)#" type="formfield">
</cfhttp>
<!--- <cfdump var="#cfhttp.filecontent#"><cfabort> --->
<!--- parse the status --->
<cfif attributes.post NEQ 'get_jurisdiction'>
<cfmodule template="#application.xti_shared_folder#xti_get_xtistatus.cfm" xmldoc="#cfhttp.filecontent#" version="#attributes.version#">
<cfelse>
	<cfset xti_get_xtistatus.actioncode="250">
	<cfset xti_get_xtistatus.ActionMessage="250">
	<cfset xti_get_xtistatus.result = True>
	<cfset xti_get_xtistatus.actiontype="Result">
</cfif>

<!--- set timing variable --->
<cfset var.MaxEnd = CreateODBCdateTime(now())>

<!--- Convert the xml to CF variables --->
<cfset tmpstruct = structnew()>
<cfset tmpstruct2 = structnew()>
<cfif (xti_get_xtistatus.result and xti_get_xtistatus.actioncode eq 250)>
	<cfset var.xmldoc = rereplacenocase(cfhttp.filecontent,"[#chr(13)##chr(10)#]","","ALL")>
	
	<!--- REMOVE THIS ONCE MAX COMPLETES THEIR WORK --->
	<cfif IsDefined("application.shared")>
	<cfmodule template="#application.shared#functions\ct_drop_xml_dtd.cfm" xml="#Trim(var.xmldoc)#" returnvar="var.xmldoc">
	</cfif> 
	<!--- REMOVE THIS ONCE MAX COMPLETES THEIR WORK --->
	
	<!--- CFMX PARSER --->
	<cfset xmldoc_in=XmlParse(Trim(var.xmldoc))>

	<cfswitch expression="#attributes.version#">
		<cfcase value="1">
			<cfset FirstKey=StructKeyArray(xmldoc_in)>
			<cfset get_wrapper_struct=evaluate("xmldoc_in." & firstKey[1])>
			<cfmodule template="#application.xti_shared_folder#xml_to_struct.cfm" xml_data="#get_wrapper_struct#" arraylist="#attributes.arraylist#" returnvar="tmpstruct">
		</cfcase>
		<cfcase value="2">
			<cfif application.site_code EQ 'ord' >
				<cfset XML2Struct = CreateObject('component', 'XML2struct') >
				<cfset tmpstruct = XML2Struct.getXML2struct(xmldoc_in.xtioutput.xmlchildren,attributes.arraylist) >
			<cfelse>
				<cfmodule template="#application.xti_shared_folder#xml_to_struct.cfm" xml_data="#xmldoc_in.xtioutput.xmlchildren#" arraylist="#attributes.arraylist#" returnvar="tmpstruct">
			</cfif>				
			<cfset tmpstruct = tmpstruct.outputdata>
		</cfcase>
		<cfcase value="3"><!--- ONLY USED FOR CHG_ORDER --->
			<cfmodule template="#application.xti_shared_folder#xml_to_struct.cfm" xml_data="#xmldoc_in.xtioutput.outputdata.xmlchildren[1].XmlChildren#" arraylist="#attributes.arraylist#" returnvar="tmpstruct_s">
			<cfmodule template="#application.xti_shared_folder#xml_to_struct.cfm" xml_data="#xmldoc_in.xtioutput.outputdata.xmlchildren[2].XmlChildren#" arraylist="#attributes.arraylist#" returnvar="tmpstruct">
			<cfoutput><cfloop collection="#tmpstruct_s#" item="i"><cfset "tmpstruct.#i#"=Evaluate('tmpstruct_s.'&i)></cfloop></cfoutput>
		</cfcase>
		<cfcase value="4"><!--- ONLY USED FOR GET_JURISDICTION --->
			<cfif isdefined("xmldoc_in.message_list.xmlchildren")>
				<!--- T129541 - loop through items in the array and process them outside xml_to_struct.cfm  --->
				<cfset jurisdictionArray=ArrayNew(1)>
				<cfloop index = "i" from = "1" to = "#arraylen(xmldoc_in.message_list.xmlchildren)#">
					<cfmodule template="#application.xti_shared_folder#xml_to_struct.cfm" xml_data="#xmldoc_in.message_list.xmlchildren[i]#" arraylist="#attributes.arraylist#" returnvar="tmpstruct" jurisdiction_flag="true">
					<cfset jurisdictionArray[i]=tmpstruct> 
					<!--- T129541: take the individual elements from the returned array jurisdictionArray and put them into 
					tempstruct3 --->
					<cfset tmpstruct3 = structnew()> 
					<cfset a = StructInsert(tmpstruct3, "CITY", jurisdictionArray[i].MESSAGE.CITY)>
					<cfset a = StructInsert(tmpstruct3, "COUNTY", jurisdictionArray[i].MESSAGE.COUNTY)>
					<cfset a = StructInsert(tmpstruct3, "GEO_CODE", jurisdictionArray[i].MESSAGE.GEO_CODE)>
					<cfset a = StructInsert(tmpstruct3, "LINE", jurisdictionArray[i].MESSAGE.LINE)>
					<cfset a = StructInsert(tmpstruct3, "OUTSIDE_CITY", jurisdictionArray[i].MESSAGE.OUTSIDE_CITY)>
					<!--- T129541 - insert tmpstruct3 as message element 1,2,3,... into tmpstruct2 (which will be returned as caller.#attributes.returnvar# later). This massaging to the struct is necessary because inc_geo_code.cfm is expecting the return struct in this specific format. --->
					<cfif i eq "1">
						<cfset a = StructInsert(tmpstruct2, "MESSAGE", tmpstruct3)>
					<cfelse>
						<cfset a = StructInsert(tmpstruct2, "MESSAGE" & #i#, tmpstruct3)>
					</cfif>
				</cfloop>
			
			<cfelseif isdefined("xmldoc_in.MAX_TI_STATUS.xmlchildren")>
				<cfmodule template="#application.xti_shared_folder#xml_to_struct.cfm" xml_data="#xmldoc_in.MAX_TI_STATUS.xmlchildren#" arraylist="#attributes.arraylist#" returnvar="tmpstruct">		
				
				<cfset xti_get_xtistatus.actiontype = "result">
				<cfset xti_get_xtistatus.actioncode = tmpstruct.error.code>
				<cfset xti_get_xtistatus.actionmessage = tmpstruct.error.message>
			</cfif>
		</cfcase>
		<cfdefaultcase>
			<cfmodule template="#application.xti_shared_folder#xml_to_struct.cfm" xml_data="#xmldoc_in.xtioutput.outputdata.xmlchildren#" arraylist="#attributes.arraylist#" returnvar="tmpstruct">
		</cfdefaultcase>
	</cfswitch>
	
	<cfif attributes.version eq "4"> <!--- ONLY USED FOR GET_JURISDICTION --->
		<cfset tmpstruct2.result = true>
	<cfelse>
		<cfset tmpstruct.result = true>
	</cfif>
<cfelse>
	<cfif attributes.version eq "4"> <!--- ONLY USED FOR GET_JURISDICTION --->
		<cfset tmpstruct2.result = false>
	<cfelse>
		<cfset tmpstruct.result = false>
	</cfif>
</cfif>

<!--- Return the variables to the calling page --->
<cfif attributes.version eq "4"> <!--- ONLY USED FOR GET_JURISDICTION --->
	<cfscript>
		tmpstruct2.actiontype = xti_get_xtistatus.actiontype;
		tmpstruct2.actioncode = xti_get_xtistatus.actioncode;
		tmpstruct2.actionmessage = xti_get_xtistatus.actionmessage;
		"caller.#attributes.returnvar#"=tmpstruct2;
	</cfscript>
<cfelse>
	<cfscript>
		tmpstruct.actiontype = xti_get_xtistatus.actiontype;
		tmpstruct.actioncode = xti_get_xtistatus.actioncode;
		tmpstruct.actionmessage = xti_get_xtistatus.actionmessage;
		"caller.#attributes.returnvar#"=tmpstruct;
	</cfscript>
</cfif>
<cfset request.timetick = GetTickCount() - request.starttick>

<cfset var.End = CreateODBCdateTime(now())>
<!--- DEBUG INFORMATION --->
<cfif attributes.debug>

	<cfoutput>
	<!--- This function hides/shows the post debug details --->
	<script language="JavaScript" type="text/javascript">
		function toggleHiddenElement( element ) {
			if ( document.getElementById( element ).style.display=="none"){
				document.getElementById( element ).style.display="";
				return true;
			} else if (document.getElementById( element ).style.display!="none"){
				document.getElementById( element ).style.display="none";
				return true;
			} 
		}
	</script> 

	<div>
		<div style="background-color:##bb99cc;padding: 0px; margin:0px;">
			<!--- click on this href to show/hide debug details --->
			<a style="color:white;text-decoration:none;cursor:hand; padding: 1px; margin:0px;" href="javascript:void(0)" onclick="toggleHiddenElement( 'details_#attributes.returnvar#_#request.starttick#' )">
				&##9678;&nbsp;XTI (input_#trim(attributes.post)#.cfm #attributes.version#): #attributes.post# &gt; [#attributes.returnvar#] = #request.timetick#ms : Click To Expand
			</a>
		</div>
		<div id="details_#attributes.returnvar#_#request.starttick#" style="display:none;">
			<b>XTI Status:</b><br>Result: #xti_get_xtistatus.result#<br> Code: #xti_get_xtistatus.actioncode#<br>Message: #xti_get_xtistatus.actionmessage#<br><br>
			<cfif ListFind(attributes.debuglevel, 1)><b><u>Level 1 -- XML Pass</u></b><br><br>#attributes.url#<br>#HtmlCodeFormat(xml)#<br></cfif>
			<cfif ListFind(attributes.debuglevel, 2)><b><u>Level 2 -- XML Return</u></b> <br>#HtmlCodeFormat(cfhttp.filecontent)#<br>
			<cfif cfhttp.filecontent eq "connection failure"><b>Header:</b><br>#CFHTTP.Header#<br></cfif> <br></cfif>
			<cfif ListFind(attributes.debuglevel, 3)><b><u>Level 3 -- Parser Engine</u></b><br><br>
				<!--- <cfdump var="#tmpstruct#" label="Struct"> --->
			</cfif>
		</div>
	</div>
	</cfoutput>
</cfif>

<!--- record the transaction times in the database --->
<cfif attributes.xti_timer eq "1">
<cfquery name="timeInsert" datasource="#request.dsn#">
	exec  asp_insert_xtimx_posttimer 
		@ipost_name = '#attributes.post#',
		@istart_time = #var.Start#,
		@iend_max_time = #var.MaxEnd#,
		@iend_time = #var.end#
</cfquery>
</cfif>